<?php
// +----------------------------------------------------------------------
// | Created by PHPstorm: [ JRK丶Admin ]
// +----------------------------------------------------------------------
// | Copyright (c) 2019~2022 [LuckyHHY] All rights reserved.
// +----------------------------------------------------------------------
// | SiteUrl: http://www.luckyhhy.cn
// +----------------------------------------------------------------------
// | Author: LuckyHhy <jackhhy520@qq.com>
// +----------------------------------------------------------------------
// | Date: 2020/6/26 0026
// +----------------------------------------------------------------------
// | Description:
// +----------------------------------------------------------------------

namespace app\admin\controller;




use app\admin\model\AdminUser;
use app\admin\model\GroupAccess;
use app\common\controller\AdminBaseController;
use app\common\model\tang\Cost;
use app\common\model\tang\CostRule;
use app\common\model\tang\OwnerMerchant as ReportDayModel;
use app\common\model\tang\Principal;

class OwnerMerchant extends AdminBaseController
{
    protected function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub
        $this->model = new ReportDayModel();
    }

    public function index()
    {
        $costModel = new Cost();
        if (IS_AJAX){
            $where = [];
            return $this->model->getAdminReportData($this->param, 'id desc',$where,static::$admin_info['nickname']);
        }
        $where = [];
        $adminInfo = session(ADMIN_LOGIN_INFO);
        if($adminInfo['id']!=1){
            $group = (new GroupAccess())->where('uid',$adminInfo['id'])->value('group_id');
            if($group!=7){
                //查找负责的商户信息
                $merchanids = (new Principal())->where('principal_id',$adminInfo['id'])->find();
                if($merchanids){//说明是主负责人
                    $where[] = ['merchant_id','in',explode(',',$merchanids['merchant_ids'])];
                }else{
                    $where[] = ['merchant_id','in',[0]];
                }
            }
            //查找负责的商户信息
//            $merchanids = (new Principal())->where('principal_id',$adminInfo['id'])->whereOr('principal','like',"%".$adminInfo['id']."%")->find();
//            if($merchanids){//说明是主负责人
//                $where[] = ['merchant_id','in',explode(',',$merchanids['merchant_ids'])];
//            }else{
//                $where[] = ['merchant_id','in',[0]];
//            }

        }
        $start['start_time'] = date('Y-m-01');
        $start['end_time'] = date('Y-m-t');
        $all_money = $costModel->where($where)->where('start_time','>=',strtotime($start['start_time']))->where('end_time','<=',strtotime($start['end_time']))->sum('all_amount');
        $all_shi = $costModel->where($where)->where('start_time','>=',strtotime($start['start_time']))->where('end_time','<=',strtotime($start['end_time']))->sum('pay_amount');
        $all_qian = bcsub($all_money,$all_shi);
        if($all_money>0){
            $data_all['all_jiaona'] = bcmul(bcdiv(bcsub($all_money,$all_qian),$all_money,2),100).'%';
        }else{
            $data_all['all_jiaona'] = '100%';
        }
        $data_all['all_money'] = $all_money/100;
        $data_all['all_shi'] = $all_shi/100;
        $data_all['all_qian'] = $all_qian/100;
        $this->assign('all_data',$data_all);
        session('all_data_merchant',$data_all);
        $merchant_list =getMerchant();
        $perListArray = (new \app\common\model\tang\Principal())->column('principal');
        if(!empty($perListArray)){
            $perList =(new AdminUser())->whereIn('id',array_unique(explode(',',$perListArray[0])))->select();
        }else{
            $perList = [];
        }
//        $perList = (new \app\common\model\tang\Principal())->with(['adminUser'])->select();
        //获取菜单列表
        $this->assign(compact("merchant_list","perList"));
        return $this->fetch();
    }
    public function export(){
        $where = [];
        $this->model->getAdminReportData($this->param, 'id desc',$where,static::$admin_info['nickname']);
    }


    public function getInfo(){
        $costModel = new Cost();
        $param = $this->request->param();
        if(!empty($param['time_type'])){
            if($param['time_type']==1){ //当前月
                $start['start_time'] = date('Y-m-01');
                $start['end_time'] = date('Y-m-t');
            }else if($param['time_type']==2){
                $M = date('m');
                if($M=='01'||$M=='02'||$M=='03'){
                    $start['start_time'] = date('Y').'-01-01';
                    $start['end_time'] = date('Y-03-t');
                }else if ($M=='04'||$M=='05'||$M=='06'){
                    $start['start_time'] = date('Y').'-03-01';
                    $start['end_time'] = date('Y-06-t');
                }else if ($M=='07'||$M=='08'||$M=='09'){
                    $start['start_time'] = date('Y').'-07-01';
                    $start['end_time'] = date('Y-09-t');
                }else{
                    $start['start_time'] = date('Y').'-10-01';
                    $start['end_time'] = date('Y-12-t');
                }
            }else if($param['time_type']==3){
                $start['start_time'] = date('Y').'-01-01';
                $start['end_time'] = date('Y').'-12-31';
            }
        }else{
            $start['start_time'] = date('Y-m-01');
            $start['end_time'] = date('Y-m-t');
        }
        $where=[];
        $adminInfo = session(ADMIN_LOGIN_INFO);
        if($adminInfo['id']!=1){
            $group = (new GroupAccess())->where('uid',$adminInfo['id'])->value('group_id');
            if($group!=7){
                //查找负责的商户信息
                $merchanids = (new Principal())->where('principal_id',$adminInfo['id'])->find();
                if($merchanids){//说明是主负责人
                    $where[] = ['merchant_id','in',explode(',',$merchanids['merchant_ids'])];
                }else{
                    $where[] = ['merchant_id','in',[0]];
                }
            }
//            //查找负责的商户信息
//            $merchanids = (new Principal())->where('principal_id',$adminInfo['id'])->find();
//            if($merchanids){//说明是主负责人
//                $where[] = ['merchant_id','in',explode(',',$merchanids['merchant_ids'])];
//            }else{
//                $where[] = ['merchant_id','in',[0]];
//            }

        }
        $all_money = $costModel->where($where)->where('start_time','>=',strtotime($start['start_time']))->where('end_time','<=',strtotime($start['end_time']))->sum('all_amount');
        $all_shi = $costModel->where($where)->where('start_time','>=',strtotime($start['start_time']))->where('end_time','<=',strtotime($start['end_time']))->sum('pay_amount');
        $all_qian = bcsub($all_money,$all_shi);
        if($all_money>0){
            $data_all['all_jiaona'] = bcmul(bcdiv(bcsub($all_money,$all_qian),$all_money,2),100).'%';
        }else{
            $data_all['all_jiaona'] = '100%';
        }
        $data_all['all_money'] = $all_money/100;
        $data_all['all_shi'] = $all_shi/100;
        $data_all['all_qian'] = $all_qian/100;
        $this->assign('all_data',$data_all);
        session('all_data_brand',$data_all);
        return self::ajaxResult($data_all,0,0,'获取成功');
    }
    /**
     * @return mixed|string|\think\response\Json
     * @throws \think\db\exception\DbException
     * @author: Hhy <jackhhy520@qq.com>
     * @date: 2020/6/30 0030
     * @describe:添加编辑
     */
    public function add()
    {
        if (IS_AJAX) {
            $data = $this->request->post();
            return $this->model->doAll($data);
        }
        $id = $this->request->param("id/d");//父id
        $info = $this->model->find($id);
        //获取菜单列表
        $this->assign(compact("info", "id"));
        return $this->fetch();
    }


}